package com.aura.lyngen.core.saver;

import com.aura.lyngen.ai.model.MultiFileCodeResult;
import com.aura.lyngen.exception.BusinessException;
import com.aura.lyngen.exception.ErrorCode;
import com.aura.lyngen.model.enums.CodeGenTypeEnum;
import org.apache.commons.lang3.StringUtils;

/**
 * @ClassName MultiFileSaverTemplate
 * @Description 多文件保存模板lei
 * @Author ZJX
 * @Date 2025/9/1 17:06
 * @Version 1.0
 **/
public class MultiFileCodeFileSaverTemplate extends CodeFileSaverTemplate<MultiFileCodeResult> {
    @Override
    protected CodeGenTypeEnum getCodeType() {
        return CodeGenTypeEnum.MULTI_FILE;
    }

    @Override
    protected void saveFiles(MultiFileCodeResult result, String baseDirPath) {
        // 保存 HTML 文件
        writeContentToFile(baseDirPath, "index.html", result.getHtmlCode());
        // 保存 CSS 文件
        writeContentToFile(baseDirPath, "style.css", result.getCssCode());
        // 保存 JavaScript 文件
        writeContentToFile(baseDirPath, "script.js", result.getJsCode());
    }

    @Override
    protected void validateInput(MultiFileCodeResult result) {
        super.validateInput(result);
        if (StringUtils.isBlank(result.getHtmlCode()) || StringUtils.isBlank(result.getCssCode()) || StringUtils.isBlank(result.getJsCode())) {
            throw new BusinessException(ErrorCode.PARAMS_NULL, "HTML、CSS、JavaScript代码不能为空");
        }
    }
}
